package com.hyt.it.ogt.ks.service.impl;

import com.hyt.core.base.BaseServiceImpl;
import com.hyt.core.util.UUIDUtils;
import com.hyt.it.ogt.ks.enums.log.AdminOperatorLogType;
import com.hyt.it.ogt.ks.enums.log.WebsocketMsgSendState;
import com.hyt.it.ogt.ks.mapper.ApplyCheckMsgMapper;
import com.hyt.it.ogt.ks.model.entity.ApplyCheck;
import com.hyt.it.ogt.ks.model.entity.ApplyCheckMsg;
import com.hyt.it.ogt.ks.service.IAnswerProgressService;
import com.hyt.it.ogt.ks.service.IApplyCheckMsgService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * <p>
 * 审核消息发送记录表 服务实现类
 * </p>
 *
 * @author zhoumiao
 * @since 2022-12-19
 */
@Service
@Slf4j
public class ApplyCheckMsgServiceImpl extends BaseServiceImpl<ApplyCheckMsgMapper, ApplyCheckMsg> implements IApplyCheckMsgService {

    @Resource
    private IAnswerProgressService iAnswerProgressService;

    @Override
    public boolean updateMsgSendState(String examId, String candidateId, int eventType, int operateType, int state, WebsocketMsgSendState msgState) {
        log.info("更新发送给学生的审核消息状态：{} {} {} {} {} {}", examId, candidateId, eventType, operateType, state, msgState);
        switch (msgState) {
            case SENDING:
                return lambdaUpdate().eq(ApplyCheckMsg::getExamId, examId)
                        .eq(ApplyCheckMsg::getCandidateId, candidateId)
                        .eq(ApplyCheckMsg::getEventType, eventType)
                        .eq(ApplyCheckMsg::getOperateType, operateType)
                        .eq(ApplyCheckMsg::getState, state)
                        .set(ApplyCheckMsg::getMsgState, msgState.getCode())
                        .update();
            case SEND_SUCCESS:
                if(operateType == AdminOperatorLogType.CANCEL_OPEN_TIMEING.getCode().intValue()) {
                    iAnswerProgressService.clearCandidateProcessRedisKey(examId, candidateId);
                }
                return lambdaUpdate().eq(ApplyCheckMsg::getExamId, examId)
                        .eq(ApplyCheckMsg::getCandidateId, candidateId)
                        .eq(ApplyCheckMsg::getEventType, eventType)
                        .eq(ApplyCheckMsg::getOperateType, operateType)
                        .eq(ApplyCheckMsg::getState, state)
                        .remove();
            default:
                log.error("消息状态错误：{}", msgState);
        }
        return false;
    }

    @Override
    public void add(ApplyCheck applyCheck, WebsocketMsgSendState msgState) {
        log.info("新增发送给学生的审核结果消息：{} {}", applyCheck, msgState);
        ApplyCheckMsg applyCheckMsg = new ApplyCheckMsg();
        applyCheckMsg.setId(UUIDUtils.randomUUID());
        applyCheckMsg.setExamId(applyCheck.getExamId());
        applyCheckMsg.setCandidateId(applyCheck.getCandidateId());
        applyCheckMsg.setEventType(applyCheck.getEventType());
        applyCheckMsg.setOperateType(applyCheck.getOperateType());
        applyCheckMsg.setState(applyCheck.getState());
        applyCheckMsg.setMsgState(msgState.getCode());
        LocalDateTime now = LocalDateTime.now();
        applyCheckMsg.setCreateDate(now);
        applyCheckMsg.setUpdateDate(now);
        applyCheckMsg.setOperateUser(applyCheck.getOperateUser());
        save(applyCheckMsg);
    }
}
